package com.eu.ebpm.engine.cmd;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eu.ebpm.engine.exception.AlreadySignExcpetion;
import com.eu.ebpm.engine.exception.ErrorCode;
import com.eu.ebpm.engine.repository.HisTaskRepository;
import com.eu.ebpm.engine.repository.TaskIdentityRepository;
import com.eu.ebpm.engine.repository.TaskRepository;

/**
 * 任务签收
 * 
 * @author 王晓华
 */
@Component
public class TaskClaimCommand {

	@Autowired
	private TaskIdentityRepository taskIdentityRepository;
	
	@Autowired
	private TaskRepository taskRepository;
	
	@Autowired
	private HisTaskRepository hisTaskRepository;

	/**
	 * 处理逻辑： 
	 * 	1、删除任务权限标志 
	 * 	2、更新签收人
	 * 
	 * @注意：
	 * 	先根据taskId查出taskIdentity的主键Id，根据主键id删除操作
	 */
	public void execute(String assignee, long taskId) {

		long resultCount = taskIdentityRepository.deleteByTaskId(taskId);

		if (resultCount <= 0) {
			throw new AlreadySignExcpetion(ErrorCode.RU_ALREADY_CLAIM);
		}

		Date assigneeTime = new Date();
		
		taskRepository.updateBySign(assignee, assigneeTime, taskId);
		
		hisTaskRepository.updateBySign(assignee, assigneeTime, taskId);

	}

}
